package biz.bbtec.sbm.store.action;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import biz.bbtec.sbm.store.bean.AddressBean;
import biz.bbtec.sbm.store.bean.CheckBean;
import biz.bbtec.sbm.store.bean.ItemBean;
import biz.bbtec.sbm.store.factory.CheckFactory;
import biz.bbtec.sbm.store.service.CheckService;
import biz.bbtec.sbm.user.bean.UserBean;

public class CheckAction extends ActionSupport {

	private static final String CHECKIN = "checkin";
	private static final String ADDCHECKIN = "addcheckin";
	private static final String CHECKOUT = "checkout";
	private static final String ADDCHECKOUT = "addcheckout";

	private int type;
	private List<CheckBean> checklist = new ArrayList<CheckBean>();
	private Map session;
	private CheckBean check;

	private List<AddressBean> addlist = new ArrayList<AddressBean>();
	private List<ItemBean> itemlist = new ArrayList<ItemBean>();
	private int itemid;
	private int adsid;

	public int getItemid() {
		return itemid;
	}

	public void setItemid(int itemid) {
		this.itemid = itemid;
	}

	public int getAdsid() {
		return adsid;
	}

	public void setAdsid(int adsid) {
		this.adsid = adsid;
	}

	public List<AddressBean> getAddlist() {
		return addlist;
	}

	public void setAddlist(List<AddressBean> addlist) {
		this.addlist = addlist;
	}

	public List<ItemBean> getItemlist() {
		return itemlist;
	}

	public void setItemlist(List<ItemBean> itemlist) {
		this.itemlist = itemlist;
	}

	public CheckBean getCheck() {
		return check;
	}

	public void setCheck(CheckBean check) {
		this.check = check;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public List<CheckBean> getChecklist() {
		return checklist;
	}

	public void setChecklist(List<CheckBean> checklist) {
		this.checklist = checklist;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public String ShowAllCheck() {
		CheckService checkserice = CheckFactory.getInstance().creattemService();
		Map session = ActionContext.getContext().getSession();
		UserBean user = (UserBean) session.get("user");
		checklist = checkserice.showallcheck(user, getType());
		if (checklist.size() == 0) {
			this.addActionError("没有任何记录！");
			return INPUT;
		} else {
			if (getType() == 0) {
				return CHECKIN;
			} else if (getType() == 1) {
				return CHECKOUT;
			} else if (getType() == 2) {
				return ADDCHECKIN;
			} else {
				return ADDCHECKOUT;
			}
		}
	}

	public String ShowALLRecorde() throws Exception {
		CheckService checkserice = CheckFactory.getInstance().creattemService();
		Map session = ActionContext.getContext().getSession();
		UserBean user = (UserBean) session.get("user");
		List<CheckBean> inlist = checkserice.showallcheck(user, 0);
		List<CheckBean> outlist = checkserice.showallcheck(user, 1);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		int i, j;
		for (i = 0, j = 0; (i < inlist.size()) && (j < outlist.size());) {
			CheckBean in = inlist.get(i);
			in.setType(0);
			CheckBean out = outlist.get(j);
			out.setType(1);

			java.util.Date inday = sdf.parse(in.getDate());
			java.util.Date outday = sdf.parse(out.getDate());

			if (inday.before(outday)) {
				checklist.add(out);
				j++;
				continue;
			} else if (outday.before(inday)) {
				checklist.add(in);
				i++;
				continue;
			} else {
				checklist.add(in);
				checklist.add(out);
				i++;
				j++;
				continue;
			}
		}
		if (i < inlist.size()) {
			for (int k = i; k < inlist.size(); k++) {
				checklist.add(inlist.get(k));
			}
		}
		if (j < outlist.size()) {
			for (int k = i; k < outlist.size(); k++) {
				checklist.add(outlist.get(k));
			}
		}
		return SUCCESS;
	}

	public String Checkin() {
		CheckService checkserice = CheckFactory.getInstance().creattemService();
		Map session = ActionContext.getContext().getSession();
		UserBean user = (UserBean) session.get("user");
		check.setItemid(getItemid());
		check.setAdsid(getAdsid());
		if (!(checkserice.checkin(check, user))) {
			this.addActionError("添加失败！");
			return INPUT;
		}
		return SUCCESS;
	}

	public String Checkout() {
		CheckService checkserice = CheckFactory.getInstance().creattemService();
		Map session = ActionContext.getContext().getSession();
		UserBean user = (UserBean) session.get("user");
		check.setItemid(getItemid());
		check.setAdsid(getAdsid());
		if (!(checkserice.checkout(check, user))) {
			this.addActionError("添加失败！请查看该货舱中货物的存储量！");
			return INPUT;
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String showallinfo() {
		CheckService checkserice = CheckFactory.getInstance().creattemService();
		Map session = ActionContext.getContext().getSession();
		UserBean user = (UserBean) session.get("user");
		itemlist = checkserice.showalliteminfo(user);
		addlist = checkserice.showalladdinfo(user);

		if (itemlist.size() == 0) {
			this.addActionError("请添加新的货物！");
			return INPUT;
		}
		if (addlist.size() == 0) {
			this.addActionError("请添加新的货仓！");
			return INPUT;
		}
		return SUCCESS;
	}
}
